unit rfidctrl;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, RzTabs, Grids, DBGrids, RzDBGrid, DB, StdCtrls, Mask,
  RzEdit, RzButton, ComCtrls, StrUtils;

type
  Trfidctrlfrm = class(TForm)
    pagectrl: TRzPageControl;
    shtRFID: TRzTabSheet;
    shtcontractor: TRzTabSheet;
    dbgridcontractor: TRzDBGrid;
    dbsource: TDataSource;
    rfidchgrcd: TRzDBGrid;
    RzBitBtn1: TRzBitBtn;
    RzBitBtn2: TRzBitBtn;
    RzBitBtn3: TRzBitBtn;
    RzBitBtn4: TRzBitBtn;
    RzBitBtn5: TRzBitBtn;
    procedure FormCreate(Sender: TObject);
    procedure RzBitBtn1Click(Sender: TObject);
    procedure rfidchgrcdDblClick(Sender: TObject);
    procedure shtRFIDShow(Sender: TObject);
    procedure shtcontractorShow(Sender: TObject);
    procedure RzBitBtn5Click(Sender: TObject);
    procedure RzBitBtn2Click(Sender: TObject);
    procedure RzBitBtn3Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    procedure addtmpRfid;
    procedure addcontractor;
    procedure editcontractor;
    procedure refreshtmprfid;
    procedure refreshcontractor;
  end;                                        

var
  rfidctrlfrm: Trfidctrlfrm;

implementation

{$R *.dfm}

Uses includefile, adtmprfid, dbmodule, edtcontractor;

procedure trfidctrlfrm.addcontractor;
var
  i:integer;
  ID:integer;
begin
  with Tedtcontractorfrm.create(application) do begin
    if Mrok=showmodal then begin
      ID:=dbmodulefrm.addConstractor(RzEdit1.Text,
                                     RzEdit2.Text,
                                     RzEdit3.Text,
                                     RzEdit4.Text);

      for i:=0 to lstctrctorpicker.Count-1 do begin
        dbmodulefrm.alterPickerConstractor(
                Trim(LeftStr(lstctrctorpicker.Items.Strings[i],10)),ID);
      end;
    end;
    refreshcontractor;
  end;
end;

procedure trfidctrlfrm.editcontractor;
var
  ID,i:integer;
begin
  with Tedtcontractorfrm.create(application) do begin
    RzEdit1.Text:=dbgridcontractor.DataSource.DataSet.FieldByName('NAME').AsString;
    RzEdit2.Text:=dbgridcontractor.DataSource.DataSet.FieldByName('LRFID').AsString;
    RzEdit3.Text:=dbgridcontractor.DataSource.DataSet.FieldByName('URFID').AsString;
    RzEdit4.Text:=dbgridcontractor.DataSource.DataSet.FieldByName('REMARK').AsString;
    ID:=dbgridcontractor.DataSource.DataSet.FieldByName('ID').AsInteger;
    dbmodulefrm.getRFIDListByConstractor(ID,lstctrctorpicker.Items);
    if Mrok=showmodal then begin
      dbmodulefrm.UpDateConstractor(RzEdit1.Text,
                                    RzEdit2.Text,
                                    RzEdit3.Text,
                                    RzEdit4.Text,
                                    ID);

      for i:=0 to lstctrctorpicker.Count-1 do begin
        dbmodulefrm.alterPickerConstractor(
                Trim(LeftStr(lstctrctorpicker.Items.Strings[i],10)),ID);
      end;
    end;
    refreshcontractor;
  end;
end;

procedure trfidctrlfrm.refreshcontractor;
begin
  with dbmodulefrm.dbquery do begin
    active:=False;
    sql.clear;
    sql.Add(format('Select NAME,Right(LRFID,%d) as LRFID,'+
                   'Right(URFID,%d) as URFID,REMARK,ID from CONTRACTOR',
                   [RFIDLEN,RFIDLEN]));
    execsql;
    active:=True;
  end;
  dbsource.DataSet:=dbmodulefrm.dbquery;
end; 

procedure trfidctrlfrm.refreshtmprfid;
begin
  with dbmodulefrm.dbquery do begin
    active:=False;
    sql.clear;
    sql.Add(format('select ID,Right(NEWRFID,%d) as NEWRFID,'+
                   'Right(OLDRFID,%d) as OLDRFID, STRATTIME,ENDTIME',
                   [RFIDLEN,RFIDLEN]));
    sql.add('from temprfid');
    execsql;
    active:=True;
  end;
  dbsource.DataSet:=dbmodulefrm.dbquery;
  dbsource.DataSet.Refresh;
end;

procedure Trfidctrlfrm.addtmpRfid;
begin
  with Tadtmprfidfrm.create(Application) do begin
    case showmodal of
      MrOk:begin
        with dbmodulefrm.dbquery do begin
          active:=False;
          sql.clear;
          sql.Add('INSERT INTO TEMPRFID ');
          sql.add('(NEWRFID,OLDRFID,TYPE,STRATTIME,ENDTIME) VALUES');
          sql.add(Format(' (''%s'',''%s'',%d,#%s#,#%s#)',
                  [RightStr('0000000000'+Trim(txttmprfid.Text),10),
                   RightStr('0000000000'+Trim(txtprimalrfid.Text),10),
                   1,
                   FormatDateTime('yyyy-MM-dd',strtime.Date),
                   FormatDateTime('yyyy-MM-dd',endtime.Date)
                   ]));
          execsql;
        end;
        dbmodulefrm.dealtmpRfid;
      end;
      MrYes:begin
        if (Trim(txttmprfid.Text)<>'') and (Trim(txtprimalrfid.Text)<>'') then begin
          dbmodulefrm.replaceRFID(Trim(txttmprfid.Text),Trim(txtprimalrfid.Text));
        end;
      end;
    end;
    refreshtmprfid;
  end;
end;

procedure Trfidctrlfrm.FormCreate(Sender: TObject);
var
  I:integer;
begin
  Caption:=GetCaption('rfidctrlfrm',1000);
  for I:=0 to ComponentCount-1 do begin
    if Components[I] is TLabel then begin
      if 0<>TLabel(Components[I]).Tag then
        TLabel(Components[I]).Caption:=
                GetCaption('rfidctrlfrm',TLabel(Components[I]).Tag);
    end;
    if Components[I] is TRzBitBtn then begin
      if 0<>TRzBitBtn(Components[I]).Tag then
        TRzBitBtn(Components[I]).Caption:=
                GetCaption('rfidctrlfrm',TRzBitBtn(Components[I]).Tag);
    end;
    if Components[I] is TRzTabSheet then begin
      if 0<>TRzTabSheet(Components[I]).Tag then
        TRzTabSheet(Components[I]).Caption:=
                GetCaption('rfidctrlfrm',TRzTabSheet(Components[I]).Tag);
    end;
  end;
  for I:=0 to 3 do begin
    rfidchgrcd.Columns[I+1].Title.Caption:=GetCaption('rfidctrlfrm',1003+I);
  end;

  for I:=0 to 3 do begin
    dbgridcontractor.Columns[I].Title.Caption:=GetCaption('rfidctrlfrm',1007+I);
  end;


end;

procedure Trfidctrlfrm.RzBitBtn1Click(Sender: TObject);
begin
  addtmpRfid;
end;

procedure Trfidctrlfrm.rfidchgrcdDblClick(Sender: TObject);
begin
  addtmpRfid;
end;

procedure Trfidctrlfrm.shtRFIDShow(Sender: TObject);
begin
  refreshtmprfid;
end;

procedure Trfidctrlfrm.shtcontractorShow(Sender: TObject);
begin
  refreshcontractor;
end;

procedure Trfidctrlfrm.RzBitBtn5Click(Sender: TObject);
var
  tmp:integer;
begin
  if IDYES=Application.MessageBox(pchar(GetCaption('Msgbox',103)),
                                  pchar(GetCaption('Msgbox',104)),MB_YESNO) then begin
    tmp:=rfidchgrcd.DataSource.DataSet['ID'];
    with dbmodulefrm.dbquery do begin
      active:=False;
      sql.clear;
      sql.Add(Format('DELETE FROM TEMPRFID WHERE ID=%d',[tmp]));
      execsql;
    end;
    refreshtmprfid;
  end;
end;

procedure Trfidctrlfrm.RzBitBtn2Click(Sender: TObject);
begin
  addcontractor;
end;

procedure Trfidctrlfrm.RzBitBtn3Click(Sender: TObject);
begin
  editcontractor;
end;

end.
